package org.korifey.ffbdd.chess;

import java.util.ArrayList;
import java.util.List;

import static org.korifey.ffbdd.chess.Position.BOARDSIZE;

public abstract class Figure implements Cloneable {
	protected Coord coord;
	protected boolean white;
	
	public abstract String shortName();
	
	public String toString() {
		return shortName() + coord;
	}
	
	public boolean isWhite() {
		return white;
	}
		
	public Coord getCoord() {
		return coord;
	}

	public void setCoord(Coord coord) {
		this.coord = coord;
	}

	public Object clone() {
		try {
			return super.clone();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public boolean canEat(Position pos, Coord c) {		
		return availableMoves(pos).contains(c);
	}

	public abstract List<Coord> availableMoves(Position pos);
	
	public static final Figure EMPTY = new Empty();
	
	static class Empty extends Figure {

		@Override
		public List<Coord> availableMoves(Position pos) {
			return new ArrayList();
		}

		@Override
		public String shortName() {
			return "E";
		}
				
	}
	
	//TODO svyazki
	//TODO pawn into queen
	static class Pawn extends Figure {
		
		public List<Coord> availableMoves(Position pos) {
			List<Coord> res = new ArrayList<Coord>();
			Figure[][] board = pos.getBoardMatrix();
			int h = coord.h(), v = coord.v();
			
			if (isWhite()) {
				
				//Move
				if (h < BOARDSIZE-1 && board[h+1][v] == EMPTY) {
					res.add(Coord.get(h+1, v));
					if (h == 1 && board[h+2][v] == EMPTY) {
						res.add(Coord.get(h+2, v));
					}
				}
				
				//Beat
				if (h < BOARDSIZE-1 && board[h+1][v-1] != EMPTY && !board[h+1][v-1].isWhite()) {
					res.add(Coord.get(h+1, v-1));
				}
				if (h < BOARDSIZE-1 && board[h+1][v+1] != EMPTY && !board[h+1][v+1].isWhite()) {
					res.add(Coord.get(h+1, v+1));
				}
				
				
			} else {
				
				//Move
				if (h > 0 && board[h-1][v] == EMPTY) {
					res.add(Coord.get(h-11, v));
					if (h == BOARDSIZE - 2 && board[h-2][v] == EMPTY) {
						res.add(Coord.get(h-2, v));
					}
				}
				
				//Beat
				if (h > 0 && board[h-1][v-1] != EMPTY && board[h-1][v-1].isWhite()) {
					res.add(Coord.get(h-1, v-1));
				}
				if (h > 0 && board[h-1][v+1] != EMPTY && board[h-1][v+1].isWhite()) {
					res.add(Coord.get(h-1, v+1));
				}
			}
			
			//Beat on passage
			if (v > 0 && board[h][v-1] != EMPTY && pos.beatOnPassage(this, coord, board[h][v-1])) {
				res.add(Coord.get(h, v-1));
			}
			if (v < BOARDSIZE-1 && board[h][v+1] != EMPTY && pos.beatOnPassage(this, coord, board[h][v+1])) {
				res.add(Coord.get(h, v+1));
			}
			
			return res;
		}
		
		public String shortName() {
			return "";
		}
		
	}
	
	
	static class Queen extends Figure {
		
		public List<Coord> availableMoves(Position pos) {
			return null;
		}
		
		public String shortName() {
			return "Q";
		}
		
	}
	
	
	static class King extends Figure {

		@Override
		public List<Coord> availableMoves(Position pos) {
			return null;
		}

		@Override
		public String shortName() {
			return "Kp";
		}
		
	}
}
